package com.atguigu.petstore.app.service.impl;
import java.math.BigDecimal;

import com.atguigu.petstore.feign.coupon.CouponFeignClient;
import com.atguigu.petstore.feign.shop.ShopFeignClient;
import com.atguigu.petstore.app.service.ShopService;
import com.atguigu.petstore.app.vo.ShopCategoryVo;
import com.atguigu.petstore.app.vo.ShopComprehensiveVo;
import com.atguigu.petstore.common.result.Result;
import com.atguigu.petstore.shop.entity.ShopCategory;
import com.atguigu.petstore.shop.entity.ShopProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author lfy
 * @Description
 * @create 2023-09-28 10:19
 */
@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    ShopFeignClient shopFeignClient;

    @Autowired
    CouponFeignClient couponFeignClient;
    @Override
    public List<ShopCategoryVo> getCategory() {

        //TODO 远程调用 shop-service 获取分类数据
        Result<List<ShopCategory>> tree =
                shopFeignClient.getShopCategoryTree();

        List<ShopCategory> data = tree.getData();

        //转换为前端用的vo模型
        List<ShopCategoryVo> vos =  converShopCategoryVoList(data);

        return vos;
    }

    @Override
    public ShopComprehensiveVo getComprehensive() {
        ShopComprehensiveVo comprehensive = new ShopComprehensiveVo();
        //1、获取广告列表: TODO 远程调用商城广告数据
        ShopComprehensiveVo.AdvertisementDTO dto = new ShopComprehensiveVo.AdvertisementDTO();
        dto.setId(1L);
        dto.setLink("http://www.baidu.com");
        dto.setImgUrl("http://192.168.200.100:9000/petstore/2023/09/28/92eea236-4a98-4ed4-abe0-22688030670a_Snipaste_2023-09-28_10-54-54.jpg");

        comprehensive.setAdvertisement(Arrays.asList(dto));

        //2、获取秒杀商品列表
        List<ShopProduct> products = shopFeignClient.getSeckillProduct().getData();
        List<ShopComprehensiveVo.ProductDTO> productDTOS = new ArrayList<>();
        //vo转化
        for (ShopProduct product : products) {
            ShopComprehensiveVo.ProductDTO productDTO = new ShopComprehensiveVo.ProductDTO();
            productDTO.setId(product.getId());
            productDTO.setPrice(product.getPrice());
            productDTO.setTitle(product.getProductName());
            productDTO.setDesc(product.getSubTitle());
            productDTO.setImage(product.getProductImg());

            productDTOS.add(productDTO);
        }
        comprehensive.setSeckill(productDTOS);

        //3、获取排行榜商品列表
        List<ShopProduct> data = shopFeignClient.getRankProduct().getData();
        List<ShopComprehensiveVo.ProductDTO> ranks = new ArrayList<>();
        for (ShopProduct product : data) {
            ShopComprehensiveVo.ProductDTO productDTO = new ShopComprehensiveVo.ProductDTO();
            productDTO.setId(product.getId());
            productDTO.setPrice(product.getPrice());
            productDTO.setTitle(product.getProductName());
            productDTO.setDesc(product.getSubTitle());
            productDTO.setImage(product.getProductImg());

            ranks.add(productDTO);
        }
        comprehensive.setRank(ranks);

        //4、获取活动列表  TODO 自己去查coupon服务的 coupon_activity
        ShopComprehensiveVo.ActiveDTO activeDTO = new ShopComprehensiveVo.ActiveDTO();
        activeDTO.setRight(Arrays.asList(new ShopComprehensiveVo.ActiveDTO.RightDTO(1L,"baidu.com",
                "http://192.168.200.100:9000/petstore/2023/09/28/92eea236-4a98-4ed4-abe0-22688030670a_Snipaste_2023-09-28_10-54-54.jpg")));
        activeDTO.setLeft(Arrays.asList(new ShopComprehensiveVo.ActiveDTO.LeftDTO(1L,"baidu.com",
                "http://192.168.200.100:9000/petstore/2023/09/28/92eea236-4a98-4ed4-abe0-22688030670a_Snipaste_2023-09-28_10-54-54.jpg")));
        comprehensive.setActive(activeDTO);

        //4、获取推荐列表； 大数据框架
        // TODO 整合大数据框架继续做实时计算。做好日志埋点。
        // 1)、每个用户访问了哪个商品，我们就日志记录，id=3 的用户访问了 id=1 的商品。
        // 2)、把日志数据保存到 hbase、hadoop、kafka，由实时计算flink、离线计算框架spark， 获取这些数据，进行分析
        // 3)、写一堆sql；  group by userid， group by cate_id，
        // 最终分析出用户喜欢哪个类目的商品，推荐这个类目相关商品给这个用户；
        // product_recommand（商品推荐表）【id, product_id, user_id, recommand_date】
        // 4)、后端查询这个表即可；
        comprehensive.setRecommend(ranks);


        return comprehensive;
    }

    private List<ShopCategoryVo> converShopCategoryVoList(List<ShopCategory> data) {
        if(data ==  null) return null;

        List<ShopCategoryVo> list = new ArrayList<>();

        // 遍历一级分类并封装
        for (ShopCategory category : data) {
            //1、创建一级分类对象
            ShopCategoryVo categoryVo = new ShopCategoryVo();

            categoryVo.setId(category.getId());
            categoryVo.setName(category.getCateName());
            categoryVo.setImage(category.getCateImg());

            //2、封装一级分类的子分类
            List<ShopCategory> children = category.getChildren();
            List<ShopCategoryVo> childrenList = new ArrayList<>();
            for (ShopCategory child : children) {
                //3、创建二级分类对象
                ShopCategoryVo vo = new ShopCategoryVo();
                //封装属性
                vo.setId(child.getId());
                vo.setName(child.getCateName());
                vo.setImage(child.getCateImg());
                vo.setChildren(null);
                //4、保存到子分类集合
                childrenList.add(vo);
            }

            categoryVo.setChildren(childrenList);

            list.add(categoryVo);
        }

        return list;
    }
}
